package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;


import model.Counter;

import model.Simulator;

/**
 * The class HistorieGramView extends AbstractView
 * 
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class HistorieGramView extends AbstractView
{
    private static final long serialVersionUID = 1L;
    private int tableWidth, tableHeight;
    Dimension size;
    private Graphics g;
    private Image fieldImage;
	
	private int xPosition;

   

    /**
     * Create a new FieldView component.
     * 
     * @param simulator
     */
	public HistorieGramView(Simulator simulator)
    {
        super(simulator);
        xPosition = 20;
        tableHeight = 300;
        tableWidth = 522;              
    }
       

    /**
     * Tell the GUI manager how big we would like to be.
     * @return new dimension
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(tableWidth, tableHeight);
    }
    

    /**
     * Prepare for a new round of painting. Since the component
     * may be resized, compute the scaling factor again.
     */
	public void preparePaint()
    {   
    	
		if(fieldImage == null) fieldImage = this.createImage(tableWidth, tableHeight);
		if(xPosition > tableWidth-25) {
			xPosition = 20;
			fieldImage = this.createImage(tableWidth, tableHeight);
		}
		g = fieldImage.getGraphics();
		
    }
    
    /**
     * updates the view
     */
	@Override
    public void updateView()
    {    
    	this.preparePaint();  		
    	HashMap<Class, Color> colors = simulator.getColors();
	            
        Iterator<HashMap<Class, Counter>> it = simulator.getStats().getHistory().iterator();
		
        while (it.hasNext()) {
        	
		    int curValue = 280; //(double)tableHeight;
		    double total = (double) simulator.getField().getWidth() * simulator.getField().getDepth();
		    HashMap<Class, Counter> counters = new HashMap<Class, Counter>();
		    counters = it.next();
		    for (Class key: counters.keySet())
		    {       	
		         Counter count = counters.get(key);		         
		         double actorCount = count.getCount();
		         double actorHeight = (actorCount / total)*280.00D;
		         //Round up with math.ceil so that there will always be 1 pixel viseble if there height is too small
		         int rectHeight = (int) Math.ceil(actorHeight);
		         curValue -= rectHeight;  
		         
		         g.setColor(colors.get(key));
		         g.fillRect(xPosition,curValue, 20,rectHeight);
		         g.setColor(Color.black);
		         g.drawString(simulator.getStep(), xPosition , 290);
		         
		    }            
	           
        }
        xPosition += 25;
        this.repaint();	 	        
    }

    /**
     * The field view component needs to be redisplayed. Copy the
     * internal image to screen.
     * @param g
     */
    public void paintComponent(Graphics g)
    {
    	
        g.drawImage(fieldImage, 0, 0, tableWidth, tableHeight, null);      
        
    }
    
    /**
     * clears the view
     */
    public void clearView()
    {
    	xPosition = 20;
    	fieldImage = this.createImage(tableWidth, tableHeight);
    }

}
